package com.msb.mall.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.msb.common.utils.PageUtils;
import com.msb.common.utils.Query;
import com.msb.common.vo.CommonPageReq;
import com.msb.mall.core.dao.product.AttrGroupDao;
import com.msb.mall.core.entity.product.AttrEntity;
import com.msb.mall.core.entity.product.AttrGroupEntity;
import com.msb.mall.core.vo.product.AttrGroupWithAttrsVo;
import com.msb.mall.core.vo.product.SpuItemGroupAttrVo;
import com.msb.mall.product.service.AttrGroupService;
import com.msb.mall.product.service.AttrService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {

    @Autowired
    private AttrService attrService;

    /**
     * 查询分页数据，根据列表编号进行查询
     *
     * @param req
     * @param catelogId 如果catelogId=0， 就不根据catelogId来查询
     * @return
     */
    @Override
    public PageUtils queryPage(CommonPageReq<Map<String, Object>> req, Long catelogId) {
        Map<String, Object> reqMap = req.getData();
        // 获取检索的关键字
        String name = (String) reqMap.get("name");

        Page<AttrGroupEntity> page = new Page<>(req.getCurrPage(), req.getPageSize());
        LambdaQueryWrapper<AttrGroupEntity> queryWrapper = new LambdaQueryWrapper<>();
//        if (StringUtils.isNotEmpty(name)) {
//            queryWrapper.and((obj) -> {
//                obj.eq(AttrGroupEntity::getAttrGroupId, name).or().like(AttrGroupEntity::getAttrGroupName, name);
//            });
//        }
        queryWrapper.like(StringUtils.isNotEmpty(name), AttrGroupEntity::getAttrGroupName, name);
        if (catelogId != 0) {
            // 根据类别编号来查询属性信息
            queryWrapper.eq(AttrGroupEntity::getCatelogId, catelogId);
        }

        IPage<AttrGroupEntity> iPageResult = baseMapper.selectPage(page, queryWrapper);
        return new PageUtils(iPageResult);
    }

    @Override
    public PageUtils queryPageOld(Map<String, Object> params, Long catelogId) {
        // 获取检索的关键字
        String key = (String) params.get("key");
        QueryWrapper<AttrGroupEntity> wrapper = new QueryWrapper<>();
        if (StringUtils.isNotEmpty(key)) {
            // 拼接查询条件
            wrapper.and((obj) -> {
                obj.eq("attr_group_id", key).or().like("attr_group_name", key);
            });
        }

        if (catelogId == 0) {
            // 不根据catalogId来查询
            IPage<AttrGroupEntity> page = this.page(
                    new Query<AttrGroupEntity>().getPage(params), wrapper
            );

            return new PageUtils(page);
        }

        // 根据类别编号来查询属性信息
        wrapper.eq("catelog_id", catelogId);
        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params), wrapper
        );
        return new PageUtils(page);
    }

    @Override
    public List<AttrGroupWithAttrsVo> getAttrgroupWithAttrsByCatelogId(Long categoryId) {
        // 根据三级分类编号查出对应的所有属性组信息
        List<AttrGroupEntity> attrGroups = baseMapper.selectList(
                new LambdaQueryWrapper<AttrGroupEntity>().eq(AttrGroupEntity::getCatelogId, categoryId));
        List<AttrGroupWithAttrsVo> list = attrGroups.stream().map(group -> {
            AttrGroupWithAttrsVo vo = new AttrGroupWithAttrsVo();
            BeanUtils.copyProperties(group, vo);
            // 根据属性组找到所有的属性信息
            List<AttrEntity> relationAttr = attrService.getRelationAttr(group.getAttrGroupId());
            vo.setAttrs(relationAttr);
            return vo;
        }).collect(Collectors.toList());

        return list;
    }

    @Override
    public List<SpuItemGroupAttrVo> getAttrgroupWithSpuId(Long spuId, Long catalogId) {
        return baseMapper.getAttrgroupWithSpuId(spuId,catalogId);
    }

}
